package cn.iocoder.yudao.module.handguard.controller.app.userfollowup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.userfollowup.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userfollowup.UserFollowUpDO;
import cn.iocoder.yudao.module.handguard.convert.userfollowup.UserFollowUpConvert;
import cn.iocoder.yudao.module.handguard.service.userfollowup.UserFollowUpService;

@Tag(name = "管理后台 - 业务员/权证跟进记录")
@RestController
@RequestMapping("/hutai/user-follow-up")
@Validated
public class UserFollowUpController {

    @Resource
    private UserFollowUpService userFollowUpService;

    @PostMapping("/create")
    @Operation(summary = "创建业务员/权证跟进记录")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:create')")
    public CommonResult<Long> createUserFollowUp(@Valid @RequestBody UserFollowUpCreateReqVO createReqVO) {
        return success(userFollowUpService.createUserFollowUp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务员/权证跟进记录")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:update')")
    public CommonResult<Boolean> updateUserFollowUp(@Valid @RequestBody UserFollowUpUpdateReqVO updateReqVO) {
        userFollowUpService.updateUserFollowUp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务员/权证跟进记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:delete')")
    public CommonResult<Boolean> deleteUserFollowUp(@RequestParam("id") Long id) {
        userFollowUpService.deleteUserFollowUp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务员/权证跟进记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:query')")
    public CommonResult<UserFollowUpRespVO> getUserFollowUp(@RequestParam("id") Long id) {
        UserFollowUpDO userFollowUp = userFollowUpService.getUserFollowUp(id);
        return success(UserFollowUpConvert.INSTANCE.convert(userFollowUp));
    }

    @GetMapping("/list")
    @Operation(summary = "获得业务员/权证跟进记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:query')")
    public CommonResult<List<UserFollowUpRespVO>> getUserFollowUpList(@RequestParam("ids") Collection<Long> ids) {
        List<UserFollowUpDO> list = userFollowUpService.getUserFollowUpList(ids);
        return success(UserFollowUpConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务员/权证跟进记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:query')")
    public CommonResult<PageResult<UserFollowUpRespVO>> getUserFollowUpPage(@Valid UserFollowUpPageReqVO pageVO) {
        PageResult<UserFollowUpDO> pageResult = userFollowUpService.getUserFollowUpPage(pageVO);
        return success(UserFollowUpConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务员/权证跟进记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:user-follow-up:export')")
    @OperateLog(type = EXPORT)
    public void exportUserFollowUpExcel(@Valid UserFollowUpExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserFollowUpDO> list = userFollowUpService.getUserFollowUpList(exportReqVO);
        // 导出 Excel
        List<UserFollowUpExcelVO> datas = UserFollowUpConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "业务员/权证跟进记录.xls", "数据", UserFollowUpExcelVO.class, datas);
    }

}
